package com.bootx.entity;

import com.fasterxml.jackson.annotation.JsonView;
import jakarta.persistence.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author black
 */
@Entity
@Table(
        indexes = {
                @Index(name = "name_time_money_batchId_index",columnList = "name,time,money,batchId",unique = true)
        }
)
public class RedPackage extends OrderedEntity<Long>{

    @JsonView(BaseEntity.PageView.class)
    private String time;

    @JsonView(BaseEntity.PageView.class)
    private String name;

    @JsonView(BaseEntity.PageView.class)
    private Double money;

    private Integer sum;

    @JsonView(BaseEntity.PageView.class)
    private Long batchId;

    private String fullTime;

    @JsonView(BaseEntity.PageView.class)
    private Long openTime;

    private Integer isOdd;

    @JsonView(BaseEntity.PageView.class)
    private Integer sn;

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getSum() {
        return sum;
    }

    public void setSum(Integer sum) {
        this.sum = sum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getBatchId() {
        return batchId;
    }

    public void setBatchId(Long batchId) {
        this.batchId = batchId;
    }

    public String getFullTime() {

        return fullTime;
    }

    public void setFullTime(String fullTime) {
        this.fullTime = fullTime;
    }

    public Long getOpenTime() {
        return openTime;
    }

    public void setOpenTime(Long openTime) {
        this.openTime = openTime;
    }

    public Integer getIsOdd() {
        return calculateMantissaSum(BigDecimal.valueOf(money));
    }

    public void setIsOdd(Integer odd) {
        isOdd = calculateMantissaSum(BigDecimal.valueOf(money));
    }

    public Integer getSn() {
        return sn;
    }

    public void setSn(Integer sn) {
        this.sn = sn;
    }

    public static Integer calculateMantissaSum(BigDecimal number) {
        // 获取小数部分
        BigDecimal mantissa = number.remainder(BigDecimal.ONE);

        // 转换为字符串处理
        String mantissaStr = mantissa.toPlainString();

        int sum = 0;
        if (mantissaStr.contains(".") && mantissaStr.length() > 2) {
            String decimalPart = mantissaStr.split("\\.")[1];
            for (char c : decimalPart.toCharArray()) {
                sum += Character.getNumericValue(c);
            }
        }
        return sum % 2;
    }
}
